package com.wf.ew.sell.controller;

import com.wf.ew.common.BaseController;
import com.wf.ew.common.JsonResult;
import com.wf.ew.common.PageResult;
import com.wf.ew.sell.model.Sell;
import com.wf.ew.sell.model.SellSon;
import com.wf.ew.sell.service.SellService;
import com.wf.ew.sell.service.SellSonService;
import com.wf.ew.system.model.Company;
import com.wf.ew.system.model.Dictionarydata;
import com.wf.ew.system.model.User;
import com.wf.ew.system.service.CompanyService;
import com.wf.ew.system.service.DictionarydataService;
import org.apache.poi.ss.formula.functions.T;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.io.IOException;
import net.sf.json.JSONArray;

/**
 *
 * controller
 * @author : Doman
 * @date : 2019-12-09 10:19:20
 * @version ：1.0
 */

@Controller
@RequestMapping("/sell/sell")
public class SellController extends BaseController {

	@Autowired
	private CompanyService companyService;
	@Autowired
	private SellService sellService;
	@Autowired
	private SellSonService sellSonService;
	@Autowired
	private DictionarydataService dictionarydataService;
	
	/**
	 * 页面
	 **/
	@RequiresPermissions("sell:view")
	@RequestMapping()
	public String sell(Model model) {
		model.addAttribute("stateList",dictionarydataService.list(new Dictionarydata("sell_state")));
		model.addAttribute("invoiceList",dictionarydataService.list(new Dictionarydata("sell_invoice")));
		return "sell/sell.html";
	}

	/**
	 * 查询列表
	 */
	@RequiresPermissions("sell:view")
	@RequestMapping(value = "/list", produces = {"text/html;charset=UTF-8;"})
	@ResponseBody
	public String list(PageResult<T> page, Sell sell, SellSon sellSon){
		return sellService.list(page, sell, sellSon, getLoginUser());
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("sell:view")
	@RequestMapping(value = "/ajaxList", produces = {"text/html;charset=UTF-8;"})
	@ResponseBody
	public String ajaxList(Sell sell){
		return JSONArray.fromObject(sellService.list(sell,getLoginUser())).toString();
	}

	/**
	 * 新增
	 **/
	@RequiresPermissions("sell:add")
	@ResponseBody
	@RequestMapping("/add")
	public JsonResult add(Model model, Sell sell) {
		return sellService.add(sell, getLoginUser());
	}

	/**
	 * 修改
	 **/
	@RequiresPermissions("sell:update")
	@ResponseBody
	@RequestMapping("/update")
	public JsonResult update(Model model, Sell sell) {
		return sellService.update(sell, getLoginUser());
	}

	/**
	 * 修改
	 **/
	@RequiresPermissions("sell:update")
	@ResponseBody
	@RequestMapping("/updateState")
	public JsonResult updateState(Model model, Sell sell) {
		return sellService.updateState(sell, getLoginUser());
	}

	/**
	 * 批量删除
	 **/
	@RequiresPermissions("sell:delete")
	@RequestMapping("/delete")
	@ResponseBody
	public JsonResult delete(Sell sell ) {
		return sellService.deleteSellAndSon(sell, getLoginUser());
	}

	/**
	 * 打印
	 **/
	@RequiresPermissions("sell:print")
	@RequestMapping("/print")
	public String print(Model model, Sell sell, SellSon sellSon) {
		model.addAttribute("sell", sellService.getById(sell));
		model.addAttribute("sellSonList",sellSonService.list(sellSon, getLoginUser()));
		User user = getLoginUser();
		Company company  = new Company();
		company.setId(user.getCompanyId());
		company = companyService.getById(company);
		model.addAttribute("user",user);
		model.addAttribute("company",company);
		return "sell/sellPrint.html";
	}

}
